package com.smartfactory.sail.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartfactory.sail.entity.DeptPost;
import com.smartfactory.sail.mapper.DeptMapper;
import com.smartfactory.sail.mapper.DeptPostMapper;
import com.smartfactory.sail.service.IDeptPostService;
import com.smartfactory.sail.vo.dept.ListAllPostResp;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 部门岗位表 服务实现类
 * </p>
 *
 * @author 王巨壁
 * @since 2019-07-24
 */
@Service
public class DeptPostServiceImpl extends ServiceImpl<DeptPostMapper, DeptPost> implements IDeptPostService {

    private DeptPostMapper deptPostMapper;
    private DeptMapper deptMapper;

    public DeptPostServiceImpl(DeptPostMapper deptPostMapper, DeptMapper deptMapper) {
        this.deptPostMapper = deptPostMapper;
        this.deptMapper = deptMapper;
    }

    @Override
    public Long add(DeptPost deptPost) {

        if (this.save(deptPost)) {
            UpdateWrapper<DeptPost> update = new UpdateWrapper<>();
            update.ge("sort", deptPost.getSort());
            update.eq("deptId", deptPost.getDeptId());
            update.setSql("sort = sort+1");
            this.update(update);
            return deptPost.getId();
        }
        return null;
    }

    @Transactional
    @Override
    public boolean del(Long id) {

        DeptPost deptPost = this.getById(id);
        if (deptPost == null) {
            return true;
        }
        //获取原来排序
        Integer sort = deptPost.getSort();
        if (this.removeById(id)) {
            UpdateWrapper<DeptPost> update = new UpdateWrapper<>();
            update.setSql("sort = sort -1");
            update.eq("deptId", deptPost.getId());
            update.ge("sort", sort);
            this.update(update);
            return true;
        }
        return false;
    }

    @Override
    public boolean updateNameOrSort(DeptPost deptPost) {
        DeptPost byId = this.getById(deptPost.getId());
        if (this.updateById(deptPost)) {
            if (deptPost.getSort() != byId.getSort()) {
                UpdateWrapper<DeptPost> update = new UpdateWrapper<>();
                if (deptPost.getSort() > byId.getSort()) {
                    //如果排序变大了. 需要把 其他between(老,新)之间的减小
                    update.setSql("sort = sort-1");
                    update.between("sort", byId.getSort(),deptPost.getSort());
                    update.eq("deptId", deptPost.getDeptId());
                } else if (deptPost.getSort() < byId.getSort()) {
                    //如果排序变小了. 需要把 其他between(新,老)之间的减小
                    update.setSql("sort = sort+1");
                    update.between("sort", deptPost.getSort(), byId.getSort());
                    update.eq("deptId", deptPost.getDeptId());
                }
                this.update(update);
                return true;
            }
        }
        return false;
    }

    @Override
    public List<ListAllPostResp> getAllPost() {

        return deptMapper.getAllPost();
    }
}
